﻿define(
    [
        'amplify',
        'utils',
        'common.consts',
        'config',
    ],
    function(amplify, utils, consts, config) {
    	var DataService = function(){
    	};
    	DataService.prototype = (function(){
		    var init = function(inResourceId, inMethod) {
            var self = this;

            self.resourceId = inResourceId;

            if (config.runMode === 'DEV') {
                self.method = 'GET';
            } else {
                if (!inMethod) {
                    self.method = 'GET';
                } else {
                    self.method = inMethod;
                }
                self.requestJsonObj = consts.API[self.resourceId];
            }

            defineAPI.call(self);
        };
        var defineAPI = function() {
            var self = this;
            var url = this.getRequestUrl();

            amplify.request.define(self.resourceId, 'ajax', {
                url : url,
                dataType : 'json',
                type : self.method,
                cache: false,
                timeout: 300000
            });
        };
        var sendRequest = function (callbacks, inParam) {
            var logger = config.logger;
            var self = this;
            var requestData = getRequestData.call(self, inParam);

            return amplify.request({
                resourceId : self.resourceId,
                success: function (data) {
                    if (data) {
                            
                        callbacks.success(data);
                    }
                    else {
                            var message = $.i18n.t("common:menuHashes.errorGettingData");
                            logger.error(message);
                        presenter.toggleActivity(false);
                    }
                },
                error : callbacks.error,
                data : {
                    request : requestData
                }
            });            
        };
        var getRequestUrl = function() {
            var self = this;
            var url = '';
            if (config.runMode === 'PRO') {
                url += consts.API_PATH.PRO;

                if (self.method === 'GET') {
                    url += "?request={request}";
                }
            } else {
                url += consts.API_PATH.DEV;
                url += '{request}'
            }

            return url;
        };
        var getLocalData = function(inParam) {};
        var getRequestData = function(inParam) {
            var self = this;
            var requestData;
            if (self.requestJsonObj) {
                var obj = utils.cloneObject(self.requestJsonObj);
                var requestParamsObj = obj[self.resourceId];
                
                if (requestParamsObj.hasOwnProperty('lcode') && requestParamsObj.lcode == null) {
                    if (inParam) {
                        if (!inParam.lcode) {
                            inParam.lcode = utils.getCurrentLanguage();
                        }                        
                        requestParamsObj = inParam;
                    } else {
                        requestParamsObj.lcode = utils.getCurrentLanguage();
                    }
                }

                obj[self.resourceId] = requestParamsObj;

                requestData = utils.jsonToString(obj);
                if(self.method == 'GET') {
                    requestData = utils.formatUriRequestData(requestData);
                }
            } else {
                requestData = self.getLocalData(inParam);
                requestData = utils.jsonToString(requestData);
            }

            return requestData;
        };
        return {
            sendRequest : sendRequest,
            init : init,
            getRequestUrl: getRequestUrl
        };
    	})();
    	return DataService;
   }
);
